package org.NooLab.openNLP.lang;



import java.util.Arrays;
import java.util.Map;
import java.util.Vector;



import org.NooLab.utilities.files.DFutils;
import org.NooLab.utilities.logging.PrintLog;
import org.NooLab.utilities.net.pages.WebRetriever;
import org.NooLab.utilities.strings.StringsUtil;

/**
 * 
 * 
 * Dornseiff categories are only available for German...
 * 
 * this class takes a user-based source and provides some means for a 
 * simple word-by-word translation;
 * 
 * the class must inherit TranslationDictSourceIntf; 
 * beyond that it may access remot sources or local executable
 * 
 * 
 * 
 */
public class WordByWordTranslation implements TranslationDictSourceIntf{


	// =================================

	// constants ......................
	String linguisticsProviderUrl = "http://en-de.dict.cc/" ;
	String requestPage = "?s=[[word]]"; // replace spaces by + -> across+the+recent+year"; 
	
	
	// object references ..............

	WebRetriever retriever ;
	
	
	// main variables / properties ....

	String configPath ;
	String workingPath ;
	
	
	// volatile variables .............
	String htmlstr;
	
	// helper objects .................
	
	PrintLog out = new PrintLog(2,true);
	StringsUtil strgutil = new StringsUtil();
	DFutils fileutil = new DFutils();
	
	// . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
	public WordByWordTranslation( String path){
		
		path = configPath;
		
	}
	// . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
	
	
	
	

	private int retrieveDictionaryEntry( String wordlabel){
		int result = -1;
		String wordPage;
		int p;
		
		
		htmlstr = "" ;
		
		wordlabel = wordlabel.trim();
		
		if (wordlabel.length()<=1){
			return result;
		}
		if (wordlabel.charAt(0)=='.'){
			return result;
		}
		
		p=strgutil.indexOfparticles(wordlabel, ".,:$&%*\"'?", 0);
		if (p>=0){
			return result;
		}
		 
		
		//wordPage = "cgi-bin/wort_www.exe?site=1&Wort=" + wordlabel.trim();
		
		wordPage = requestPage.replace("[[word]]", wordlabel.trim() ) ;
		//                         "http://dict.uni-leipzig.de/"
		retriever = new WebRetriever( linguisticsProviderUrl,	wordPage); 
	
		retriever.setPrintLevel( out.getPrintlevel() ) ;
		retriever.setSaveRawPagestoTmp(true);
		// this causes the WebRetriever to store the results in a local temp dir
		retriever.setUseSystemTmp(false);
		retriever.setTempPathBase(configPath) ;
		retriever.setEmptyPagePattern("Es wurden keine Ergebnisse gefunden.");
		retriever.setPolitenessDelay(1200); // default = 800 millis
		
		// retriever.setExpiryAge(100.0); // in days, as a float, default is 100 days 
		// and not to use the system temp dir for that
		// if it is used locally, the external page will not be contacted again, 
		// except the expiryAge () has been reached
		
		htmlstr = retriever.getHtml(2); // 2 -> explicitly using the get method, which is mandatory for scripted sites,
		                                //      where we call a script in a cgi-bin directory
	
		
		retriever = null;
		if (htmlstr.length()<5){
			result = -3;
			return result;
		}else{
			result=0;
		}
		
		htmlstr = retriever.htmlCorrection(htmlstr);
		
		
	
		return result;
	}
	
	
	// ------------------------------------------------------------------------
	
}
